package jp.co.apl.haven_enquete.android.activity;

import java.security.KeyStore;

import jp.co.apl.haven_enquete.android.R;
import jp.dev7.android.util.Layouts;
import jp.dev7.android.worker.Worker;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestActivity extends Activity {

    private HttpClient httpClient = new DefaultHttpClient();
    private HttpContext httpContext = new BasicHttpContext();
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        setContentView(layout, Layouts.getParamFillFill());

        Button button = new Button(this);
        button.setText("click");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // connect("https://my-enquete.googlecode.com/hg/");
                    connect("https://10.0.2.2/test/");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        layout.addView(button, Layouts.getParamFillWrap());

        textView = new TextView(this);
        layout.addView(textView, Layouts.getParamFillWrap());

    }

    private void connect(final String url) throws Exception {
        Log.d("haven", "click!");

        textView.setText("");

        Worker<String> w = new Worker<String>() {
            @Override
            protected String doInBackground() throws Exception {
                Log.d("haven", "start background proccess");

                KeyStore trustStore =
                    KeyStore.getInstance(KeyStore.getDefaultType());
                trustStore.load(
                    getResources().openRawResource(R.raw.keystore),
                    "narazaki".toCharArray());

                SSLSocketFactory sf = new SSLSocketFactory(trustStore);
                sf
                    .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

                Scheme https = new Scheme("https", sf, 443);
                httpClient
                    .getConnectionManager()
                    .getSchemeRegistry()
                    .register(https);

                HttpGet get = new HttpGet(url);

                Log.d("haven", "send request");
                HttpResponse response = httpClient.execute(get, httpContext);
                Log.d("haven", "receive response");

                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String res = EntityUtils.toString(entity);
                    Log.d("haven", "response = " + res);
                    return res;
                }
                Log.d("haven", "response = " + null);
                return null;
            }

            @Override
            protected Message done() {
                Message msg = new Message();
                Bundle data = new Bundle();
                data.putString(Worker.RESULT, getResult());
                msg.setData(data);
                return msg;
            }
        };

        Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                String text = msg.getData().getString(Worker.RESULT);
                textView.setText(text);
                return true;
            }
        });
        w.execute(handler);
    }
}
